package com.util;

import java.util.LinkedList;
import java.util.List;

public class PageMaker<T> {
	private int currentPage;
	private int pageSize;
	public PageMaker(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getPageSize() {
		return pageSize;
	}
	public List<T> getCurrentPageList(List<T> list, int currentPage) {
		int totalPage = list.size() / pageSize;
		if (list.size() % pageSize != 0) {
			totalPage ++;
		}
		if (currentPage > totalPage - 1) {
			currentPage = totalPage - 1;
		}
		if (currentPage < 0) {
			currentPage = 0;
		}
		this.currentPage = currentPage;
		List<T> output = new LinkedList<T>();
		int firstEle = currentPage * pageSize;
		int pageEle = (list.size() - firstEle) >= pageSize ? pageSize : (list
				.size() - firstEle);
		for (int i = firstEle; i < firstEle + pageEle; i++) {
			output.add(list.get(i));
		}
		return output;
	}

	public int getCurrentPage() {
		return currentPage;
	}

}
